/*
 * SharedMem.h
 * 
 * Author: Tarl Hahn
 * Version: 1.0
 *
 * SharedMem is the shared memory area between CPU and the interrupt Controller.
 * This area facilitates communications by holding the io requests from the processes
 * and all the interrupts created by system devices and the timer.
 */
#ifndef SHAREDMEM_H
#define SHAREDMEM_H

#include <pthread.h>

typedef struct data
{
	int pid; // process ID
	int did; // device ID
	struct data* next; // next
	
	// member functions
	int (*destruct)(struct data*);     // destructs the data pack
	int (*destructAll)(struct data*);  // destructs the list recursively
	char* (*toString)(struct data*, int*); // toString
	
} DataPack;

typedef DataPack* DataPackPtr; // DataPack pointer def

DataPackPtr DataPackConstruct(int pid, int did, DataPackPtr next); // constructs a DataPack node for the sharedmem queue.
 
typedef struct shared
{
	DataPackPtr request_list; // array containing device number and process number that owns it.
	DataPackPtr interrupt_list; // array containg the device number and process number that it is returned to
	DataPackPtr int_head; // head of interrupt list
	DataPackPtr int_tail; // tail of interrupt list
	DataPackPtr req_head; // head of request list
	DataPackPtr req_tail; // tail of request list
	
	int timer_flag;     // if 1 timer interrupt, 0 otherwise.
	int int_count;      // length of the interrupt list
	int req_count;      // length of the  request list
	int run_flag;       // flag indicating run status
	int (*addRequest)(struct shared*, int pid, int did, pthread_mutex_t*); // add a request to the queue
	int (*addInterrupt)(struct shared*, int pid, int did, pthread_mutex_t*); // add a interrupt to the queue
	int (*getRequest)(struct shared*, pthread_mutex_t*, int*); // get a request for the queue
	int (*getInterrupt)(struct shared*, pthread_mutex_t*, int*); // get an interrupt from the queue
	int (*setTimer)(struct shared*, int, pthread_mutex_t*);  // set timer to the specified value
	int (*destruct)(struct shared*); // destruct the shared memory area
	char* (*toStringInt)(struct shared*, int*); // toString for interrupt Queue
	char* (*toStringReq)(struct shared*, int*); // toString for request Queue
	
} SharedMem;

typedef SharedMem* SharedMemPtr; // SharedMem pointer def

SharedMemPtr SharedMemConstruct(void);
#endif
 